unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, XPMenu, ExtCtrls, Menus, jpeg;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    GroupBox1: TGroupBox;
    GroupBox3: TGroupBox;
    StatusBar1: TStatusBar;
    Label5: TLabel;
    addr: TComboBox;
    showm: TRichEdit;
    PopupMenu1: TPopupMenu;
    Clear1: TMenuItem;
    Save1: TMenuItem;
    SelectAll1: TMenuItem;
    N1: TMenuItem;
    Copy1: TMenuItem;
    Paste1: TMenuItem;
    Cut1: TMenuItem;
    SaveDialog1: TSaveDialog;
    GroupBox2: TGroupBox;
    Label6: TLabel;
    Label18: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    GroupBox8: TGroupBox;
    Label7: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label9: TLabel;
    Label13: TLabel;
    Label16: TLabel;
    Label15: TLabel;
    Label14: TLabel;
    Label8: TLabel;
    Label17: TLabel;
    GroupBox4: TGroupBox;
    reqa: TRadioButton;
    reqi: TRadioButton;
    GroupBox5: TGroupBox;
    keyb: TRadioButton;
    keya: TRadioButton;
    Button3: TButton;
    ComboBox4: TComboBox;
    ComboBox6: TComboBox;
    ComboBox5: TComboBox;
    Panel1: TPanel;
    GroupBox6: TGroupBox;
    reqaa: TRadioButton;
    reqii: TRadioButton;
    GroupBox7: TGroupBox;
    keybb: TRadioButton;
    keyaa: TRadioButton;
    Button4: TButton;
    ComboBox9: TComboBox;
    ComboBox8: TComboBox;
    ComboBox7: TComboBox;
    Edit3: TEdit;
    Panel2: TPanel;
    Button29: TButton;
    Panel72: TPanel;
    nohalt: TRadioButton;
    yeshalt: TRadioButton;
    Panel71: TPanel;
    reql5: TRadioButton;
    reqa5: TRadioButton;
    Label81: TLabel;
    procedure checkdata(s:string);
    procedure falsereason(s:string);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Clear1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure SelectAll1Click(Sender: TObject);
    procedure Copy1Click(Sender: TObject);
    procedure Paste1Click(Sender: TObject);
    procedure Cut1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button29Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function API_OpenComm(com:string;baudrate:integer):integer;stdcall;external 'mi.dll';
function API_CloseComm(comHandle:Thandle ):integer;stdcall;external 'mi.dll';

function API_SetDeviceAddress(comHandle : Thandle;DeviceAddress : integer;newAddr:integer; buffer: pinteger):integer;stdcall;
external 'mi.dll';

function API_SetBandrate(comHandle : Thandle;DeviceAddress : integer;newBaud:integer; buffer: pinteger):integer;stdcall;
external 'mi.dll';

function API_SetSerNum(comHandle : Thandle;DeviceAddress : integer;newvalue:pchar; buffer: pchar):integer;stdcall;
external 'mi.dll';

function API_GetSerNum(comHandle : Thandle;DeviceAddress : integer;buffer: pchar):integer;stdcall;
external 'mi.dll';

function API_PCDRead(comHandle:Thandle;   DeviceAddress:integer;  mode:byte; add_blk,num_blk:integer; snr,buffer:pchar):integer;stdcall;
external 'mi.dll';

function API_PCDWrite(comHandle:Thandle;   DeviceAddress:integer;  mode:byte; add_blk,num_blk:integer; snr,buffer:pchar):integer;stdcall;
external 'mi.dll';


function GET_SNR(comHandle:Thandle;   DeviceAddress:integer;  halt,mode:byte;  snr,value:pchar):integer;stdcall;
external 'mi.dll';

var
  Form1: TForm1;
  comhandle : Thandle ;
  s100,reason:string;
implementation

{$R *.dfm}

procedure TForm1.checkdata(s:string);   //过虑数据中的空格和其它不合法字符
var                                     //所有过虑好的字符都放在S100里面
 temp:string;
 i:integer;
begin
for i:=1 to length(s) do
    begin
    if ((copy(s,i,1)>='0') and (copy(s,i,1)<='9'))or((copy(s,i,1)>='a') and (copy(s,i,1)<='f'))
        or((copy(s,i,1)>='A') and (copy(s,i,1)<='F')) then
      begin
        temp:=temp+copy(s,i,1);
      end;
    end;

    s100:='';

     for i:=0 to length(temp) div 2 - 1 do
      begin
       s100:=s100+copy(temp,(2*i+1),2);
      end;
end;

procedure TForm1.falsereason(s:string);
begin
     if s=''   then begin reason:=''; exit; end;
     if s='00' then begin reason:=''; exit ; end;
     if s='1'  then begin reason:='Command false.....';   exit; end;
     if s='2'  then begin reason:='checksum error.....';  exit; end;
     if s='3'  then begin reason:='Not selected COM port.....';  exit; end;
     if s='4'  then begin reason:='time out reply.....';       exit; end;
     if s='5'  then begin reason:='check sequence error.....';    exit; end;
     if s='7'  then begin reason:='check sum error.....';   exit; end;
     if s='10' then begin reason:='the parameter value out of range.....';  exit; end;
     if s='80' then begin reason:='Command OK.....';    exit; end;
     if s='81' then begin reason:='Command FAILURE....';   exit; end;
     if s='82' then begin reason:='Reader reply time out error....';  exit; end;
     if s='83' then begin reason:='the card do not exist....';     exit; end;
     if s='84' then begin reason:='the data is error....';   exit; end;
     if s='85' then begin reason:='Reader received unknown command....';      exit; end;
     if s='87' then begin reason:='error....';    exit; end;
     if s='89' then begin reason:='The parameter of the command or the Format of the command Erro...';   exit; end;
     if s='8A' then begin reason:='Some Erro appear in the card InitVal process...';      exit; end;
     if s='8B' then begin reason:='Get The Wrong Snr during anticollison loop....';       exit; end;
     if s='8C' then begin reason:='The authentication failure....';    exit; end;
     if s='8F' then begin reason:='Reader received unknown command....';   exit; end;
     if s='90' then begin reason:='The Card do not support this command....';   exit; end;
     if s='91' then begin reason:='The Foarmat Of  The Command Erro....';        exit; end;
     if s='92' then begin reason:='Do not support Option mode....';     exit; end;
     if s='93' then begin reason:='The Block Do Not Exist....';   exit; end;
     if s='94' then begin reason:='The Object have been locked....';   exit; end;
     if s='95' then begin reason:='The lock Operation Do Not Success....';  exit; end;
     if s='96' then begin reason:='The Operation Do Not Success....';   exit; end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  comhandle:=0;
  addr.Text:='00';
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 buffer :array [0..255] of char;
 receive,i :integer;
 Sernum:string;
 key : pchar;
begin

    s100:=edit2.Text;
    checkdata(s100);   //过虑数据中的多余的字符

    setlength(Sernum,8);      //转化数据

    for i:=1 to 8 do
    begin
      Sernum[i]:=Chr(StrToInt('$'+Copy(s100,2*i-1,2)));
    end;

    key:=StrPCopy(buffer,Sernum);

 receive:=API_SetSerNum(comhandle,strtoint(addr.Text),key,@buffer[1]);
 if (receive=0) then
  begin
   showm.Lines.Add('success');
   falsereason(inttohex(ord(buffer[1]),2));
   showm.Lines.Add(reason+#13#10);
  end
 else
  begin
   if (receive=null) then begin  showm.Lines.Add('error,no data receive...'+#13#10);  exit; end;
   if (receive<>null) then
    begin
     showm.Lines.Add('false');
     falsereason(inttostr(receive));
     showm.Lines.Add(reason);
     reason:='';
     falsereason(inttohex(ord(buffer[1]),2));
     showm.Lines.Add(reason+#13#10);
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
 receivedata :string;
 receive,i:integer;
  buffer:array [1..6] of integer;
  buffer1:array [0..63] of char;
begin

  receive:= API_GetSerNum(comhandle,strtoint(addr.Text),buffer1);
  if (receive=0) then
  begin
   showm.Lines.Add('success');
   for i:=1 to 8 do
    begin
     receivedata:=receivedata+ inttohex(ord(buffer1[i]),2)+' ';
    end;
     showm.Lines.Add(receivedata+#13#10);
  end
 else
  begin
   if (receive=null) then begin  showm.Lines.Add('error,no data receive...'+#13#10);  exit; end;
   if (receive<>null) then
    begin
     showm.Lines.Add('false');
     falsereason(inttostr(receive));
     showm.Lines.Add(reason);
     reason:='';
     falsereason(inttohex(ord(buffer[1]),2));
     showm.Lines.Add(reason+#13#10);
    end;
   end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
 mode:byte;
 add_blk,num_blk:byte;
 buffer,bufferr:array [0..255] of char;
 receive,i :integer;
 keydata,cardnum,carddata,showmemo:string;
 key : pchar;
begin
  if reqi.Checked and keya.Checked then mode:=$00;
  if reqi.Checked and keyb.Checked then mode:=$02;
  if reqa.Checked and keya.Checked then mode:=$01;
  if reqa.Checked and keyb.Checked then mode:=$03;
  add_blk:=strtoint('$'+inttohex(strtoint(ComboBox5.Text),2));
  num_blk:=strtoint('$'+inttohex(strtoint(ComboBox6.Text),2));

   s100:=ComboBox4.Text;
   checkdata(s100);  //过虑数据中的多余的字符
   setlength(keydata,6);      //转化数据
    for i:=1 to 6 do
    begin
      keydata[i]:=Chr(StrToInt('$'+Copy(s100,2*i-1,2)));
    end;
    key:=StrPCopy(buffer,keydata);    //得到指针

    receive:=API_PCDRead(comhandle,strtoint(addr.Text),mode,add_blk,num_blk,key,bufferr);
//读出来的东西放在 buffer（ 4个字节的卡号）和 bufferr 里面 （ 读取到的数据(具体数量为： num_blk*16)）

    if receive=0 then
     begin
      showm.Lines.Add('success..');
      for i:=0 to 3  do
       begin
        cardnum:=cardnum+ inttohex(ord(buffer[i]),2)+' ';
       end;
      showm.Lines.Add('the receive card number is :'+cardnum);
      for i:=0 to strtoint('$'+inttohex(num_blk,2))*16-1 do
       begin
        carddata:=carddata+ inttohex(ord(bufferr[i]),2)+' ';
       end;
      showm.Lines.Add('the card data is ：'+carddata+#13#10);
     end

    else

     begin
     if (receive=null) then begin  showm.Lines.Add('error,no data receive...'+#13#10);  exit; end;
     if (receive=1) then
      begin
       showm.Lines.Add('false');
       showmemo:=inttohex(ord(bufferr[0]),2);
       falsereason(inttohex(ord(bufferr[0]),2));
       showm.Lines.Add(reason+#13#10);
      end
     else
      begin
       falsereason(inttostr(receive));
       showm.Lines.Add('false,the reason is :'+reason+#13#10);
      end;
     end;
end;

procedure TForm1.Clear1Click(Sender: TObject);
begin
  showm.Clear;
end;

procedure TForm1.Save1Click(Sender: TObject);
begin
  savedialog1.Title:='Save as！' ;
  SaveDialog1.filter:='text(.txt)|*.txt';
  if(savedialog1.Execute()) then
  showm.Lines.SaveToFile(SaveDialog1.filename);
end;

procedure TForm1.SelectAll1Click(Sender: TObject);
begin
  showm.SelectAll;
end;

procedure TForm1.Copy1Click(Sender: TObject);
begin
  showm.CopyToClipboard;
end;

procedure TForm1.Paste1Click(Sender: TObject);
begin
  Showm.PasteFromClipboard;
end;

procedure TForm1.Cut1Click(Sender: TObject);
begin
  showm.CutToClipboard;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
 mode:byte;
 add_blk,num_blk:byte;
 buffer,bufferr:array [0..255] of char;
 receive,i :integer;
 keydata,writedata,cardnum:string;
 key,keywrite : pchar;
begin
  if reqii.Checked and keyaa.Checked then mode:=$00;
  if reqii.Checked and keybb.Checked then mode:=$02;
  if reqaa.Checked and keyaa.Checked then mode:=$01;
  if reqaa.Checked and keybb.Checked then mode:=$03;
  add_blk:=strtoint('$'+inttohex(strtoint(ComboBox7.Text),2));
  num_blk:=strtoint('$'+inttohex(strtoint(ComboBox8.Text),2));
    //开始处理密钥匙
   s100:=ComboBox9.Text;
   checkdata(s100);  //过虑数据中的多余的字符
   setlength(keydata,6);      //转化数据
    for i:=1 to 6 do
    begin
      keydata[i]:=Chr(StrToInt('$'+Copy(s100,2*i-1,2)));
    end;
    key:=StrPCopy(buffer,keydata);    //得到指针
      //结束处理密钥

      //开始处理待写数据
   S100:='';
   s100:=Edit3.Text;
   checkdata(s100);  //过虑数据中的多余的字符
   setlength(writedata,num_blk*16);      //转化数据
   if length(s100)<>num_blk*32 then
    begin
     showmessage('input error,the data length is not accord,please checkout again.. ');
     exit;
    end;
    for i:=1 to 16*num_blk do
    begin
      writedata[i]:=Chr(StrToInt('$'+Copy(s100,2*i-1,2)));
    end;

    keywrite:=StrPCopy(bufferr,writedata);    //得到指针
      //结束处理待写数据

    receive:=API_PCDWrite(comhandle,strtoint(addr.Text),mode,add_blk,num_blk,key,keywrite);
//读出来的东西放在 buffer（ 4个字节的卡号）里面

    if receive=0 then
     begin
      showm.Lines.Add('success..');
      for i:=0 to 3  do
       begin
        cardnum:=cardnum+ inttohex(ord(buffer[i]),2)+' ';
       end;
      showm.Lines.Add('the receive card number is :'+cardnum+#13#10);
     end

    else

     begin
     if (receive=null) then begin  showm.Lines.Add('error,no data receive...'+#13#10);  exit; end;
     if (receive=1) then
      begin
       showm.Lines.Add('false');
       falsereason(inttohex(ord(bufferr[0]),2));
       showm.Lines.Add(reason+#13#10);
      end
     else
      begin
       falsereason(inttostr(receive));
       showm.Lines.Add('false,the reason is :'+reason+#13#10);
      end;
     end;
  end;
procedure TForm1.Button29Click(Sender: TObject);
var
 mode,halt:byte;
 snr,value:array [0..255] of char;
 receive,i :integer;
 cardnum,carddata,showmemo:string;
 key : pchar;
begin
  if reql5.Checked  then  mode:=$26
                    else  mode:=$52;

  if nohalt.Checked then  halt:=$00
                    else  halt:=01;

    receive:=GET_SNR(comhandle,strtoint(addr.Text),mode,halt,snr,value);
   // GET_SNR(hComm, devAddr, mode, half, asnr(0), aBuffer(0))

    if receive=0 then
     begin
      showm.Lines.Add('success..'+FormatDateTime('hh:mm:ss AM/PM', Now()));
     
      for i:=0 to 3  do
       begin
        cardnum:=cardnum+ inttohex(ord(value[i]),2)+' ';
       end;
      carddata:=inttohex(ord(snr[0]),2);



      if carddata='00' then  showm.Lines.Add('It is only one card...')
                       else  showm.Lines.Add('It is more than one card...');

      showm.Lines.Add('the receive card number is :'+cardnum);
      showm.Lines.Add('');

     end

    else

     begin
     if (receive=null) then begin  showm.Lines.Add('error,no data receive...'+FormatDateTime('hh:mm:ss AM/PM', Now())+#13#10);  exit; end;
     if (receive=1) then
      begin
       showm.Lines.Add('false'+FormatDateTime('hh:mm:ss AM/PM', Now()));
       showmemo:=inttohex(ord(snr[0]),2);
       falsereason(inttohex(ord(snr[0]),2));
       showm.Lines.Add(reason+#13#10);
      end
     else
      begin
       falsereason(inttostr(receive));
       showm.Lines.Add('false,the reason is :'+reason+FormatDateTime('hh:mm:ss AM/PM', Now())+#13#10);
      end;
     end;
end;

end.


